import { login, logout } from '@/api/admin'
import {getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import {AccessToken,RefreshToken} from '@/utils/token'


const state = {
  access_token: getToken(AccessToken),
  name: '',
  avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
  user: Object,
  roles: [],
  button: []
}


const mutations = {
  SET_ACCESS_TOKEN: (state, token) => {
    state.access_token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_BUTTON: (state, button) => {
    state.button = button
  },
  SET_USER: (state, user) => {
    state.user = user
    state.avatar = user.headImage
  },
  RESET_STATE: (state) => {
    state.roles = []
    state.button = []
    state.name = ''
    state.access_token = ''
    state.user = null
    removeToken(AccessToken)
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo

    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {

        const { data } = response
        const { accessToken} = data

        console.log('accessToken：' + accessToken)

        commit('SET_ACCESS_TOKEN', accessToken)
        setToken(AccessToken,accessToken)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.access_token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { roles, userName, user ,permissions} = data

        // 取出roleArray
        const roleArray = []
        roles.forEach(role=>{
          roleArray.push(role.name)
        })

        // 取出按钮权限
        const button = []
        permissions.forEach(permission=>{
          button.push(permission.name)
        })

        commit('SET_NAME', userName)
        commit('SET_ROLES', roleArray)
        commit('SET_BUTTON', button)
        commit('SET_USER', user)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      console.log('触发登出')
      logout(state.access_token).then(() => {
        removeToken(AccessToken) // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(AccessToken) // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  // setUser
  setUser({commit},user){
    console.log("触发更新 user")
    commit('SET_USER', user)
    console.log("触发更新 user 成功")
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
